import request from '@/utils/request';
import { RequestConfig } from '@/utils/http/types';

// 用户相关 API
export class UserService {
  // 获取用户信息
  static getUserInfo() {
    return request.get<any>('/user/info');
  }

  // 用户登录
  static login(data: { username: string; password: string }) {
    return request.post<{ token: string }>('/user/login', data);
  }

  // 用户注册
  static register(data: { username: string; password: string; email: string }) {
    return request.post<{ token: string }>('/user/register', data);
  }

  // 修改用户信息
  static updateUserInfo(data: any) {
    return request.put<any>('/user/info', data);
  }

  // 登出
  static logout() {
    return request.post<void>('/user/logout');
  }
}

// 表格数据相关 API
export class TableService {
  // 获取表格数据
  static getTableData(params?: any, config?: RequestConfig) {
    return request.get<any>('/table/list', params, {
      ...config,
      // 可选配置，比如显示 loading
      showLoading: true,
      loadingText: '加载表格数据中...'
    });
  }

  // 获取表格详情
  static getTableDetail(id: string | number) {
    return request.get<any>(`/table/detail/${id}`);
  }

  // 添加表格数据
  static addTableData(data: any) {
    return request.post<any>('/table/add', data);
  }

  // 更新表格数据
  static updateTableData(id: string | number, data: any) {
    return request.put<any>(`/table/update/${id}`, data);
  }

  // 删除表格数据
  static deleteTableData(id: string | number) {
    return request.delete<any>(`/table/delete/${id}`);
  }

  // 批量删除表格数据
  static batchDeleteTableData(ids: (string | number)[]) {
    return request.post<any>('/table/batch-delete', { ids });
  }
}

// 文件上传相关 API
export class FileService {
  // 上传单个文件
  static uploadFile(file: File, data?: Record<string, any>) {
    return request.upload<any>('/file/upload', file, 'file', data, {
      showLoading: true,
      loadingText: '上传文件中...'
    });
  }

  // 上传多个文件
  static uploadMultipleFiles(files: File[], data?: Record<string, any>) {
    const formData = new FormData();
    
    files.forEach((file, index) => {
      formData.append(`files[${index}]`, file);
    });
    
    if (data) {
      Object.keys(data).forEach(key => {
        formData.append(key, data[key]);
      });
    }
    
    return request.post<any>('/file/upload-multiple', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      showLoading: true,
      loadingText: '上传文件中...'
    });
  }

  // 下载文件
  static downloadFile(fileId: string) {
    return request.download(`/file/download/${fileId}`);
  }
}

// Mock 数据服务 (用于本地开发)
export class MockService {
  // 获取用户数据
  static getUserData() {
    return request.get<any>('/mock/user.json', undefined, {
      useMock: true
    });
  }

  // 获取角色数据
  static getRoleData() {
    return request.get<any>('/mock/role.json', undefined, {
      useMock: true
    });
  }

  // 获取表格数据
  static getTableData() {
    return request.get<any>('/mock/table.json', undefined, {
      useMock: true
    });
  }
} 